// ===============================================
// env_config
//
// Create by will on 2021/10/16 12:10
// Copyright @Will.All rights reserved.
// ===============================================

// 环境配置

import 'app_policies.dart';

class EnvConfig {
  const EnvConfig({
    this.appTitle,
    this.baseUrl,
    this.keyStr,
    this.ivStr,
  });

  //环境名称
  final String? appTitle;

  //服务器地址
  final String? baseUrl;

  //测试 秘钥
  final String? keyStr;

  //解码
  final String? ivStr;
}

// 获取的配置信息
class Env {
  Env();

  // 获取到当前环境
  String appEnv = String.fromEnvironment(EnvName.envKey);

  EnvConfig get envConfig => _getEnvConfig();

// 根据不同环境返回对应的环境配置
  EnvConfig _getEnvConfig() {
    final baseUrl = AppPolicies.apiBaseURL ?? '';

    final debug = EnvConfig(
      appTitle: 'debug',
      baseUrl: baseUrl,
      keyStr: 'qlZ4F3jiLgTMoLkl+p9DdxFjdNXmXl/z3u6wl3AxMCw=',
      ivStr: 'P8GmSsx5wlFAKq7ilMqfZQ==',
    );

    final release = EnvConfig(
      appTitle: 'release',
      baseUrl: baseUrl,
      keyStr: 'okbTC43qNnznxBcdxLCug8uUVZnoYZbN6S2HGjmLa7o=',
      ivStr: 'P8GmSsx5wlFAKq7ilMqfZQ==',
    );

    switch (appEnv) {
      //开发环境
      case EnvName.debug:
        return debug;
      //开发环境 线上 没显示错误日志的
      case EnvName.debugNoLog:
        return debug;
      //正式环境
      case EnvName.release:
        return release;
      //正式环境 线上 有显示错误日志的
      case EnvName.releaseLog:
        return release;
      default:
        return debug;
    }
  }
}

// 声明的环境
abstract class EnvName {
  // 环境key
  static const String envKey = 'APP_ENV';

  //开发环境 不显示错误日志的
  static const String debug = 'debug';

  //开发环境 有显示错误日志的
  static const String debugNoLog = 'debugNoLog';

  //正式环境 不显示错误日志的
  static const String release = 'release';

  //正式环境 有显示错误日志的
  static const String releaseLog = 'releaseLog';
}

// 是否正式服(api用)
bool isRelease() {
  const appEnv = String.fromEnvironment(EnvName.envKey);
  return appEnv == EnvName.release || appEnv == EnvName.releaseLog;
}

// 是否测试(api用)
bool isTesting() {
  const appEnv = String.fromEnvironment(EnvName.envKey);
  return appEnv == EnvName.debug || appEnv == EnvName.debugNoLog;
}

// 是否打印(打印用)
bool isErrorLog() {
  const appEnv = String.fromEnvironment(EnvName.envKey);
  return appEnv == EnvName.debugNoLog || appEnv == EnvName.releaseLog;
}
